package com.yst.gateway.monitor.services.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yst.gateway.monitor.common.utils.DateUtils;
import com.yst.gateway.monitor.common.utils.LoginUserCache;
import com.yst.gateway.monitor.common.utils.UUIDGenerator;
import com.yst.gateway.monitor.dao.QzApplicationMapper;
import com.yst.gateway.monitor.models.QzApplication;
import com.yst.gateway.monitor.services.QzAppsService;

@Service
@Scope("prototype")
public class QzAppsServiceImpl implements QzAppsService {

	@Autowired
	private QzApplicationMapper qzApplicationMapper;
	 
	@Override
	public boolean add(QzApplication t) {
		t.setCtime(DateUtils.getNowTime());
		t.setCreator(LoginUserCache.getUser().getSysUserName());
		t.setStatus(2);
		if (qzApplicationMapper.insert(t) > 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean update(QzApplication app) {
		app.setUtime(DateUtils.getNowTime());
		app.setModifier(LoginUserCache.getUser().getSysUserName());	 
		int result = qzApplicationMapper.updateByPrimaryKey(app);
		if(result > 0) {
			return true;
		}else {
			return false;			
		}
	}

	@Override
	public QzApplication getById(String id) {
		return qzApplicationMapper.selectByPrimaryKey(id);
	}


	@Override
	public boolean updateStatusById(String id) {
		boolean flag=false;
		QzApplication t=getById(id);
		if(t!=null){
			QzApplication upApp=new QzApplication();
			upApp.setAppId(id);
			upApp.setStatus(t.getStatus()==1?2:1);
			upApp.setUtime(DateUtils.getNowTime());
			upApp.setModifier(LoginUserCache.getUser().getSysUserName());
			flag=qzApplicationMapper.updateStatus(upApp)>0?true:false;
		}
		return flag;
	}

	@Override
	public boolean delById(String id) {
		QzApplication app = new QzApplication();
		app.setAppId(id);
		app.setStatus(0);
		app.setUtime(DateUtils.getNowTime());
		app.setModifier(LoginUserCache.getUser().getSysUserName());
		int result = qzApplicationMapper.updateStatus(app);
		if(result > 0) {
			return true;
		}else {
			return false;			
		}
	}
	
	
	
	@Override
	public PageInfo<QzApplication> getByPage(int pageNum, int pageSize,Map<String, Object> paramMap) {
		PageHelper.startPage(pageNum, pageSize);
		return new PageInfo<QzApplication>(qzApplicationMapper.selectAll(paramMap));
	}

	@Override
	public QzApplication checkName(Map<String, String> paramMap) {		
		return qzApplicationMapper.checkName(paramMap);
	}

	@Override
	public QzApplication checkCode(Map<String, String> paramMap) {
		return qzApplicationMapper.checkCode(paramMap);
	}

	@Override
	public List<QzApplication> getByMap(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<QzApplication> getIntersById(String id) {
		return qzApplicationMapper.getIntersById(id);
	}

	@Override
	public List<QzApplication> getMatchInters(Map<String, Object> paramMap) {
		return qzApplicationMapper.getMatchInters(paramMap);
	}

	@Override
	public boolean associateInter(Map<String, Object> paramMap) {
		paramMap.put("appInterId", UUIDGenerator.getUUID());
		if (qzApplicationMapper.associateInter(paramMap) > 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean assoDel(String id) {
		return qzApplicationMapper.assoDel(id);
	}

}
